package com.devenv.repository;

import com.devenv.entity.Software;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

/**
 * 软件信息Repository
 * 提供基础的软件信息查询功能
 */
@Repository
public interface SoftwareRepository extends JpaRepository<Software, Long> {

    /**
     * 根据软件名称查找
     */
    Optional<Software> findByName(String name);

    /**
     * 检查软件名称是否存在
     */
    boolean existsByName(String name);

    /**
     * 根据状态查找所有软件并按下载量排序
     */
    List<Software> findAllByStatusOrderByDownloadCountDesc(Software.SoftwareStatus status);

    /**
     * 分页查询所有软件（按下载量排序）
     */
    Page<Software> findAllByStatusOrderByDownloadCountDesc(Software.SoftwareStatus status, Pageable pageable);

    /**
     * 根据分类查找软件列表
     */
    List<Software> findByCategory(String category);

    /**
     * 根据分类和状态查找软件列表
     */
    List<Software> findByCategoryAndStatusOrderByDownloadCountDesc(String category, Software.SoftwareStatus status);

    /**
     * 根据操作系统支持查找软件
     */
    @Query(value = "SELECT * FROM software s WHERE s.status = 'ACTIVE' AND s.deleted = false AND " +
           "JSON_CONTAINS(s.os, JSON_QUOTE(?1))", nativeQuery = true)
    List<Software> findByOs(@Param("os") String os);

    /**
     * 根据操作系统支持和状态查找软件
     */
    @Query(value = "SELECT * FROM software s WHERE s.status = ?2 AND s.deleted = false AND " +
           "JSON_CONTAINS(s.os, JSON_QUOTE(?1)) ORDER BY s.download_count DESC", nativeQuery = true)
    List<Software> findByOsContainingAndStatusOrderByPriorityDesc(@Param("os") String os, @Param("status") String status);

    /**
     * 分页查询 - 根据分类和操作系统
     */
    @Query(value = "SELECT * FROM software s WHERE " +
           "(:category IS NULL OR s.category = :category) AND " +
           "(:os IS NULL OR JSON_CONTAINS(s.os, JSON_QUOTE(:os))) AND " +
           "s.status = 'ACTIVE' AND s.deleted = false " +
           "ORDER BY s.download_count DESC", nativeQuery = true)
    Page<Software> findByCategoryAndOsWithPage(@Param("category") String category, 
                                               @Param("os") String os, 
                                               Pageable pageable);

    /**
     * 查找所有分类
     */
    @Query("SELECT DISTINCT s.category FROM Software s WHERE s.status = 'ACTIVE' AND s.deleted = false ORDER BY s.category")
    List<String> findAllCategories();

    /**
     * 根据关键词搜索软件
     */
    @Query(value = "SELECT * FROM software s WHERE s.status = 'ACTIVE' AND s.deleted = false AND " +
           "(s.name LIKE CONCAT('%', :keyword, '%') OR s.description LIKE CONCAT('%', :keyword, '%')) " +
           "ORDER BY s.download_count DESC", nativeQuery = true)
    List<Software> searchByKeyword(@Param("keyword") String keyword);

    /**
     * 分页搜索软件
     */
    @Query(value = "SELECT * FROM software s WHERE s.status = 'ACTIVE' AND s.deleted = false AND " +
           "(s.name LIKE CONCAT('%', :keyword, '%') OR s.description LIKE CONCAT('%', :keyword, '%')) AND " +
           "(:category IS NULL OR s.category = :category) AND " +
           "(:os IS NULL OR JSON_CONTAINS(s.os, JSON_QUOTE(:os))) " +
           "ORDER BY s.download_count DESC", nativeQuery = true)
    Page<Software> searchByKeywordWithPage(@Param("keyword") String keyword,
                                          @Param("category") String category,
                                          @Param("os") String os,
                                          Pageable pageable);

    /**
     * 统计相关查询
     */
    long countByStatus(Software.SoftwareStatus status);

    @Query("SELECT COUNT(DISTINCT s.category) FROM Software s WHERE s.status = 'ACTIVE' AND s.deleted = false")
    long countDistinctCategories();

    @Query("SELECT COALESCE(SUM(s.downloadCount), 0) FROM Software s WHERE s.status = 'ACTIVE' AND s.deleted = false")
    long sumDownloadCounts();
}
